const { baseUrl } = require('../utils/env');
const user = require('../utils/user')

function uploadFile(tempFilePaths) {
  return new Promise((resolve, reject) => {
    wx.uploadFile({
      url: baseUrl + '/file/upload',
      filePath: tempFilePaths,
      name: 'file',
      formData: {
        file: tempFilePaths,
      },
      header: {
        'Content-Type': 'application/json',
        'Access-Token': user.getToken()
      },
      success(res) {
        const data = JSON.parse(res.data)
        if (data.code == 200) {
          resolve(data.data)
        } else {
          reject(data)
        }
      },
      fail(err) {
        reject(err)
      }
    })
  })

}

module.exports = {
  uploadFile
}